import UIKit

//   ██
// ██
class Block4: Block {
    /**
     * 构造方法。
     */
    public override init() {
        super.init()
        mBlocks[1][1].mUsed = true
        mBlocks[1][2].mUsed = true
        mBlocks[2][0].mUsed = true
        mBlocks[2][1].mUsed = true
    }
    
    /**
     * 获取底端所在的行数。底端是指方块最下方的有效格。
     * @return 底端所在的行数
     */
    public override var bottom: Int {
        return mRow + 2
    }
    
    /**
     * 擦除。
     */
    public override func erase() {
        var rect: CGRect!

        switch mShape {
            case .ONE:
                rect = CGRect(x: Const.GRID_SIZE * (mLine + 1), y: Const.GRID_SIZE * (mRow + 1), width: Const.GRID_SIZE * 2, height: Const.GRID_SIZE)
                super.eraseRect(rect)
                rect = CGRect(x: Const.GRID_SIZE * (mLine), y: Const.GRID_SIZE * (mRow + 2), width: Const.GRID_SIZE * 2, height: Const.GRID_SIZE)
                super.eraseRect(rect)
            case .TWO:
                rect = CGRect(x: Const.GRID_SIZE * (mLine + 1), y: Const.GRID_SIZE * (mRow), width: Const.GRID_SIZE , height: Const.GRID_SIZE * 2)
                super.eraseRect(rect)
                rect = CGRect(x: Const.GRID_SIZE * (mLine + 2), y: Const.GRID_SIZE * (mRow + 1), width: Const.GRID_SIZE , height: Const.GRID_SIZE * 2)
                super.eraseRect(rect)
            default:
                break
        }
    }
    
    /**
     * 对不能下落的处理。
     */
    internal override func handleCannotMoveDown() {
        switch mShape {
            case .ONE:
                Game.sExistentBlocks[mRow + 1][mLine + 1].mUsed = true
                Game.sExistentBlocks[mRow + 1][mLine + 2].mUsed = true
                Game.sExistentBlocks[mRow + 2][mLine].mUsed = true
                Game.sExistentBlocks[mRow + 2][mLine + 1].mUsed = true
                Game.sExistentBlocks[mRow + 1][mLine + 1].mColor = mColor
                Game.sExistentBlocks[mRow + 1][mLine + 2].mColor = mColor
                Game.sExistentBlocks[mRow + 2][mLine].mColor = mColor
                Game.sExistentBlocks[mRow + 2][mLine + 1].mColor = mColor
            case .TWO:
                Game.sExistentBlocks[mRow][mLine + 1].mUsed = true
                Game.sExistentBlocks[mRow + 1][mLine + 1].mUsed = true
                Game.sExistentBlocks[mRow + 1][mLine + 2].mUsed = true
                Game.sExistentBlocks[mRow + 2][mLine + 2].mUsed = true
                Game.sExistentBlocks[mRow][mLine + 1].mColor = mColor
                Game.sExistentBlocks[mRow + 1][mLine + 1].mColor = mColor
                Game.sExistentBlocks[mRow + 1][mLine + 2].mColor = mColor
                Game.sExistentBlocks[mRow + 2][mLine + 2].mColor = mColor
            default:
                break
        }
    }

    /**
     * 下落。
     * @return 成功则返回 true，否则返回 false
     */
    public override func moveDown() -> Bool {
        switch mShape {
            case .ONE:
                if mRow == 17 {
                    self.handleCannotMoveDown()
                    return false
                }
                if Game.sExistentBlocks[mRow + 3][mLine].mUsed ||
                    Game.sExistentBlocks[mRow + 3][mLine + 1].mUsed ||
                    Game.sExistentBlocks[mRow + 2][mLine + 2].mUsed {
                    self.handleCannotMoveDown()
                    return false
                }
            case .TWO:
                if mRow == 17 {
                    self.handleCannotMoveDown()
                    return false
                }
                if Game.sExistentBlocks[mRow + 2][mLine + 1].mUsed ||
                    Game.sExistentBlocks[mRow + 3][mLine + 2].mUsed {
                    self.handleCannotMoveDown()
                    return false
                }
            default:
                break
        }

        return super.moveDown()
    }
    
    /**
     * 左移。
     */
    public override func moveLeft() {
        switch mShape {
            case .ONE:
                if mLine == 0 {
                    return
                }
                if Game.sExistentBlocks[mRow + 1][mLine].mUsed ||
                    Game.sExistentBlocks[mRow + 2][mLine - 1].mUsed {
                    return
                }
            case .TWO:
                if mLine == -1 {
                    return
                }
                if Game.sExistentBlocks[mRow][mLine].mUsed ||
                    Game.sExistentBlocks[mRow + 1][mLine].mUsed ||
                    Game.sExistentBlocks[mRow + 2][mLine + 1].mUsed {
                    return
                }
            default:
                break
        }

        super.moveLeft()
    }
    
    /**
     * 右移。
     */
    public override func moveRight() {
        switch mShape {
            case .ONE:
                if mLine == 7 {
                    return
                }
                if Game.sExistentBlocks[mRow + 1][mLine + 3].mUsed ||
                    Game.sExistentBlocks[mRow + 2][mLine + 2].mUsed {
                    return
                }
            case .TWO:
                if mLine == 7 {
                    return
                }
                if Game.sExistentBlocks[mRow][mLine + 2].mUsed ||
                    Game.sExistentBlocks[mRow + 1][mLine + 3].mUsed ||
                    Game.sExistentBlocks[mRow + 2][mLine + 3].mUsed {
                    return
                }
            default:
                break
        }

        super.moveRight()
    }
    
    /**
     * 获取顶端所在的行数。顶端是指方块最上方的有效格。
     * @return 顶端所在的行数
     */
    public override var top: Int {
        if mShape == .ONE {
            return mRow + 1
        }
        return mRow
    }
    
    /**
     * 变形。
     */
    public override func transform() {
        switch mShape {
            case .ONE:
                if mRow == -1 {
                    return
                }
                if Game.sExistentBlocks[mRow][mLine + 1].mUsed ||
                    Game.sExistentBlocks[mRow + 2][mLine + 2].mUsed {
                    return
                }
                self.erase()
                mBlocks[2][0].mUsed = false
                mBlocks[2][1].mUsed = false
                mBlocks[0][1].mUsed = true
                mBlocks[2][2].mUsed = true
                mShape = .TWO
            case .TWO:
                if mLine == -1 {
                    return
                }
                self.erase()
                mBlocks[2][0].mUsed = true
                mBlocks[2][1].mUsed = true
                mBlocks[0][1].mUsed = false
                mBlocks[2][2].mUsed = false
                mShape = .ONE
            default:
                break
        }
        
        super.draw()
    }
}
